package com.system.handler;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.system.base.MyTask;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 定时任务
 * 
 * @author kaifa002
 *
 */
@Slf4j
@Component
public class MyScheduled {

	@Autowired
	private RedisTemplate<String, Object> redisTemplate;
	@Autowired
	private ThreadPoolTaskExecutor threadPoolTaskExecutor;

	/**
	 * 定时任务处理在redis里面保存的任务
	 */
	// @Scheduled(cron = "0/2 * * * * ?")
	public void execute() throws InterruptedException {
		log.info("执行定时任务：从redis获取task任务,放入到线程池中");
		String task = (String) redisTemplate.opsForList().leftPop("threadPoolTask");
		if (task == null) {
			return;
		}
		log.info(task);
		// JSONObject jsonObject = JSONObject.parseObject(task);
		// MyTask myTask = new MyTask("redis-001", jsonObject.getString("taskeName"), jsonObject.getLong("orderId"), jsonObject.getInteger("resourceType"));
		MyTask myTask = JSONUtil.toBean(task, MyTask.class);
		myTask.setTaskId("redis-" + myTask.getTaskId());
		threadPoolTaskExecutor.execute(myTask);
		
	}

}
